#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化版同步交易日期客户端
用于访问指定日期前160天的数据
"""

import requests
import datetime
import time
from urllib.parse import quote

def sync_trade_dates_simple():
    """简化版同步函数"""
    
    # 配置参数 0007910F7554787 0007910F8879450
    base_url = "http://192.168.31.188:8131"
    shop_code = "0007910F8879450"
    tenant = "1"
    end_date = "2025-07-15"
    days_back = 360
    
    print(f"开始同步交易日期数据...")
    print(f"店铺代码: {shop_code}")
    print(f"租户: {tenant}")
    print(f"结束日期: {end_date}")
    print(f"向前推天数: {days_back}")
    print("-" * 50)
    
    # 生成日期范围
    end_dt = datetime.datetime.strptime(end_date, "%Y-%m-%d")
    success_count = 0
    fail_count = 0
    
    for i in range(days_back):
        # 计算当前日期
        current_date = end_dt - datetime.timedelta(days=i)
        date_str = current_date.strftime("%Y-%m-%d %H:%M:%S")
        
        # 构建URL
        encoded_date = quote(date_str)
        url = f"{base_url}/syncTradeDate?shopCode={shop_code}&tenant={tenant}&date={encoded_date}"
        
        print(f"处理第 {i+1}/{days_back} 个请求: {date_str}")
        
        try:
            # 发送请求
            response = requests.get(url, timeout=30)
            response.raise_for_status()
            
            print(f"  ✓ 成功 (状态码: {response.status_code})")
            success_count += 1
            
        except requests.exceptions.RequestException as e:
            print(f"  ✗ 失败: {e}")
            fail_count += 1
        
        # 添加延迟避免请求过于频繁
        if i < days_back - 1:
            time.sleep(0.1)  # 100ms延迟
    
    # 打印结果
    print("-" * 50)
    print(f"同步完成!")
    print(f"总请求数: {days_back}")
    print(f"成功请求数: {success_count}")
    print(f"失败请求数: {fail_count}")
    print(f"成功率: {success_count/days_back*100:.2f}%")

if __name__ == "__main__":
    sync_trade_dates_simple() 